package com.dz.common.web;

import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import lombok.extern.slf4j.Slf4j;

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import com.alibaba.fastjson.JSON;

@ControllerAdvice
@Slf4j
public class GlobalDefaultExceptionHandler {

	@ExceptionHandler(value = Exception.class)
	public void defaultErrorHandler(HttpServletRequest req, HttpServletResponse response, Exception e)
			throws IOException {
		log.error("error for :"+req.getRequestURI(),e);
		response.setContentType(MediaType.APPLICATION_JSON_UTF8_VALUE);
		ResultVO resultVO = new ResultVO(500, null, e.getMessage());
		OutputStream out = response.getOutputStream();
		out.write(JSON.toJSONBytes(resultVO));
		out.close();
	}
}
